package com.example.base.app.impl;

import com.example.base.app.AppClassService;
import com.example.base.system.SysAppService;
import com.example.base.system.domain.SysApp;
import com.example.base.util.AppClassLoader;
import com.example.base.util.URLClassLoaderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class AppClassServiceImpl implements AppClassService {

    @Autowired
    private SysAppService sysAppService;


    @Override
    public List<Class<?>> getClassByAppName(String appName) {
        SysApp sysApp=sysAppService.getSysAppByAppName(appName);
        URLClassLoader urlClassLoader= URLClassLoaderUtils.getClassLoaderMap().get(sysApp.getAppFilename());
        List<Class<?>> classList=new ArrayList<>();
        if(urlClassLoader!=null){
            if(urlClassLoader instanceof AppClassLoader){
                AppClassLoader appClassLoader=(AppClassLoader) urlClassLoader;
                Map<String, Class<?>> classMap= appClassLoader.getLoadedClasses();
                classList.addAll(classMap.values());
            }
        }
        return classList;
    }
}
